package Back;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class MAP {
   public int width;
   public int height;
    public String map;
    public int carcount;
    public  int max_time;
    public String[][] allpath;//记录小车的路径
    public int[][] lighten;//照亮区域
    public int[][] map_obstacle;//地图静态数据
    public List<Point>[] carPositions; // 存储每辆小车的位置历史
    public List<String> maplighstate;//存储地图的点亮状态。
    //绘制成二维数组咯！！！！
   public MAP(int width,int height,String map,int carcount,int max_time)
    {
        this.width=width;
        this.height=height;
        this.map=map;
        this.carcount=carcount;
        this.max_time=max_time;
        this.map_obstacle=createmap();
        this.lighten=createlight();
    }
    //将字符串转为二维数组
    public int[][] createmap()
    {
        int[][] arr=new int[height][width];
        for(int y=0;y<height;y++)
        {
            for(int x=0;x<width;x++)
            {
                int index=y*width+x;
                if(map.charAt(index)=='0')
                    arr[y][x]=0;
                else
                    arr[y][x]=1;
            }
        }
        return arr;
    }
    //初始化点亮区域
    public int[][] createlight()
    {
        int[][] arr=new int[height][width];
        for(int y=0;y<height;y++)
        {
            for(int x=0;x<width;x++)
            {
                arr[y][x]=0;
            }
        }
        return arr;
    }

    //点亮九宫格
    /**
     * 在照亮区域地图上点亮以 (x, y) 为中心的九宫格区域
     */
    public void lightarea(int x, int y) {
        for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {
                int dx = x + i;
                int dy = y + j;
                // 边界检查
                if (dx >= 0 && dx < width && dy >= 0 && dy < height) {
                    lighten[dy][dx] = 1;
                }
            }
        }
    }

    //初始化车辆位置
    public void initializeCarPositions() {
        carPositions = new ArrayList[this.carcount + 1];
        for (int i = 1; i <= this.carcount; i++) {
            carPositions[i] = new ArrayList<>();
            Point lastValidPosition = null; // 记录上一个有效的坐标
            for (int t = 0; t <= this.max_time; t++) {
                if (this.allpath[i][t] != null) {
                    String[] coords = this.allpath[i][t].split(" ");
                    if (coords.length == 2) {
                        try {
                            int x = Integer.parseInt(coords[0]);
                            int y = Integer.parseInt(coords[1]);
                            Point pos = new Point(x, y);
                            carPositions[i].add(pos);
                            lastValidPosition = pos; // 更新最后有效位置
                            continue;
                        } catch (NumberFormatException e) {
                            // 解析失败
                        }
                    }
                }
                carPositions[i].add(lastValidPosition); // ✅ 保留上一次的坐标
            }
        }
    }

    //记录点亮的数组状态
    public void initemaplight() {
        maplighstate = new ArrayList<>();

        // 初始化全 '0' 字符串
        StringBuilder initial = new StringBuilder();
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                initial.append('0');
            }
        }
        maplighstate.add(initial.toString());

        // 按时间轴构建点亮状态
        for (int t = 1; t <= max_time; t++) {
            StringBuilder prevState = new StringBuilder(maplighstate.get(t - 1));

            for (int i = 1; i <= carcount; i++) {
                Point pos = getCarPosition(i, t);
                if (pos != null) {
                    prevState = lighting(pos, prevState); // 更新当前帧状态
                }
            }

            maplighstate.add(prevState.toString());
        }
    }


    //获取小车数量
    public Point getCarPosition(int carId, int time) {
        if (carId < 1 || carId > this.carcount) return null;
        if (time < 0 || time >this.max_time) return null;

        return carPositions[carId].get(time);
    }
    //点亮九宫格
    /**
     * 点亮以 pos 为中心的九宫格区域，并返回一个新的 StringBuilder 实例
     */
    public StringBuilder lighting(Point pos, StringBuilder sta) {
        StringBuilder newSta = new StringBuilder(sta); // 复制一份避免修改原对象
        int x = pos.x;
        int y = pos.y;
        for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {
                int dx = x + i;
                int dy = y + j;
                if (dx >= 0 && dx < this.width && dy >= 0 && dy < this.height) {
                    int index = dy * this.width + dx;
                    newSta.setCharAt(index, '1');
                }
            }
        }

        return newSta;
    }

}
